package phoeics.authoryserver.webservice.restcontroller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import phoeics.authoryserver.webservice.entity.OauthUser;
import phoeics.authoryserver.webservice.repository.OauthUserRepository;
import phoeics.authoryserver.webservice.restcontroller.base.BaseRestController;
import phoenics.common.BaseData.StatusData;
import phoenics.common.exception.ParamsException;

@RestController
@RequestMapping("/auth")
public class UserWebService extends BaseRestController{
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(UserWebService.class);
	@Autowired
	OauthUserRepository oauthUserRepository;
	@RequestMapping(value = "/user/register", method = RequestMethod.POST)
	public StatusData register(@RequestBody OauthUser user){
		String name=user.getUsername();
		if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
			throw new ParamsException();
		}
		OauthUser oauthUser=oauthUserRepository.findByUsername(name);
		if(oauthUser!=null){
			throw new ParamsException();
		}
		OauthUser oauthUser_=oauthUserRepository.save(user);
		if(oauthUser_!=null){
			return new StatusData(0,"ok");
		}else{
			throw new ParamsException();
		}
	}
}
